package com.baozou.app.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.baozou.R;
import com.baozou.framework.base.BaseActivity;

public class ImageViewDemoActivity extends BaseActivity {
	ImageView iv;

	ZoomControls zc;
	Matrix matrix = new Matrix();
	Matrix savedMatrix = new Matrix();
	float downX,downY,upX,upY,scale=1f; 
	int displayWidth,displayHeight;
	Bitmap b = null;
	boolean isCenterX=true,isCenterY=true;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		displayWidth = dm.widthPixels;
		displayHeight = dm.heightPixels - 50;
		Log.d("dd", "displayWidth:" + displayWidth + ",displayHeight:" + displayHeight);
		
		b = BitmapFactory.decodeResource(getResources(), R.drawable.test);
		int w = b.getWidth();
		int h = b.getHeight();
		Log.d("dd", "w:" + w + ",h:" + h);
		
		float f1 = displayWidth/(w + 0f);
		float f2 = displayHeight/(h + 0f);
		if(w > displayWidth && h > displayHeight){
			if(f1 < f2)
				scale = f1;
			else
				scale = f2;
		}else if(w > displayWidth){
			scale = f1;
		}else if(h > displayHeight){
			scale = f2;
		}
		
		iv = (ImageView)findViewById(R.id.image_image);
		
		Log.d("dd", "scale:" + scale);
		matrix.postScale(scale, scale);
		iv.setImageMatrix(matrix);
		savedMatrix.set(matrix);
		Log.d("matrix", matrix.toString());
		float[] f = new float[9];
		
		Log.d("matrix", matrix.toString());
		
		center();
		Log.d("matrix", matrix.toString());
		matrix.getValues(f);
		Log.d("matrix", f[Matrix.MTRANS_X]+"");
		zc = (ZoomControls)findViewById(R.id.image_zoom);
		zc.setIsZoomOutEnabled(true);
		zc.setIsZoomOutEnabled(true);
		zc.setOnZoomInClickListener(new ZoomControls.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				matrix.set(savedMatrix);
				matrix.postScale(1.25f,1.25f,displayWidth/2,displayHeight/2);
				scale *= 1.25f;
				iv.setImageMatrix(matrix);
				savedMatrix.set(matrix);
			}
		});
		
		zc.setOnZoomOutClickListener(new ZoomControls.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				matrix.set(savedMatrix);
				matrix.postScale(0.75f,0.75f,displayWidth/2,displayHeight/2);
				scale *= 0.75f;
				iv.setImageMatrix(matrix);
				
				float[] f = new float[9];
				matrix.getValues(f);
				
				if(!isCenterX && b.getWidth()*scale < displayWidth+1){
					matrix = new Matrix();
					matrix.postScale(scale,scale);
					matrix.postTranslate((displayWidth-b.getWidth()*scale)/2,f[Matrix.MTRANS_Y]);
					iv.setImageMatrix(matrix);
					
					isCenterX = true;
				}else if(b.getWidth()*scale >= displayWidth+1){
					float x = 0f;
					if(f[Matrix.MTRANS_X] > 0)
						x = 0 - f[Matrix.MTRANS_X];
					if(f[Matrix.MTRANS_X] < -(b.getWidth() * scale - displayWidth))
						x = -f[Matrix.MTRANS_X] - (b.getWidth() * scale - displayWidth);
					
					matrix.postTranslate(x,0);
					iv.setImageMatrix(matrix);
				}
				
				matrix.getValues(f);
				if(!isCenterY && b.getHeight()*scale < displayHeight+1){
					matrix = new Matrix();
					matrix.postScale(scale,scale);
					matrix.postTranslate(f[Matrix.MTRANS_X],(displayHeight - b.getHeight()*scale)/2);
					iv.setImageMatrix(matrix);
					
					isCenterY = true;
				}else if(b.getHeight()*scale >= displayHeight+1){
					float y = 0f;
					if(f[Matrix.MTRANS_Y]+y > 0)
						y = 0 - f[Matrix.MTRANS_Y];
					if(f[Matrix.MTRANS_Y]+y < -(b.getHeight() * scale - displayHeight))
						y = -f[Matrix.MTRANS_Y] - (b.getHeight() * scale - displayHeight);
					matrix.postTranslate(0,y);
					iv.setImageMatrix(matrix);
				}
				
				savedMatrix.set(matrix);
			}
		});
		
		iv.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub
				switch(e.getAction() & MotionEvent.ACTION_MASK){
					case MotionEvent.ACTION_DOWN:
						downX = e.getX();
						downY = e.getY();
						matrix.set(savedMatrix);
						break;
					case MotionEvent.ACTION_UP:
						float[] f = new float[9];
						matrix.set(savedMatrix);
						matrix.getValues(f);
						
						float bw = b.getWidth() * scale;
						float bh = b.getHeight() * scale;
						isCenterX = false;
						isCenterY = false;
						upX = e.getX(); 
						upY = e.getY();
						float x = upX-downX;
						float y = upY-downY;
						
						if(bw < displayWidth+1)
							x = 0;
						if(bh < displayHeight+1)
							y = 0;
						if(x != 0){
							if(f[Matrix.MTRANS_X]+x > 0)
								x = 0 - f[Matrix.MTRANS_X];
							if(f[Matrix.MTRANS_X]+x < -(bw - displayWidth))
								x = -f[Matrix.MTRANS_X] - (bw - displayWidth);
						}
						if(y != 0){
							if(f[Matrix.MTRANS_Y]+y > 0)
								y = 0 - f[Matrix.MTRANS_Y];
							if(f[Matrix.MTRANS_Y]+y < -(bh - displayHeight))
								y = -f[Matrix.MTRANS_Y] - (bh - displayHeight);
						}
						
						
						matrix.postTranslate(x, y);
						iv.setImageMatrix(matrix);
						savedMatrix.set(matrix);
						break;
					case MotionEvent.ACTION_MOVE:
						float spaceX = e.getX() - downX;
						float spaceY = e.getY() - downY;
						if(!(spaceX > 10 || spaceX < -10))
							spaceX = 0;
						if(!(spaceY > 10 || spaceY < -10))
							spaceY = 0;
						
						if(b.getWidth()*scale < displayWidth+1)
							spaceX = 0;
						if(b.getHeight()*scale < displayHeight+1)
							spaceY = 0;
						
						drag(spaceX,spaceY);
						break;
				}
				return true;
			}
		});
	}
	
	public void drag(float spaceX,float spaceY){
		Log.d("dd", "drag spaceX:"+spaceX + ",spaceY:" + spaceY);

		matrix.set(savedMatrix);

		matrix.postTranslate(spaceX, spaceY);
		iv.setImageMatrix(matrix);
	}
	
	public void center(){
		float x = (displayWidth - b.getWidth()*scale)/2;
		float y = (displayHeight - b.getHeight()*scale)/2;
		
		matrix.set(savedMatrix);

		matrix.postTranslate(x, y);
		iv.setImageMatrix(matrix);
		
		savedMatrix.set(matrix);
	}
}
